using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Animoso.Domain;
using Animoso.Domain.Data;

namespace Animoso.Web.Controllers
{
    public class AlbumController : ApplicationController
    {
        private IRepository<Album> _albumRepository;

        public AlbumController(IRepository<Album> albumRepository)
        {
            _albumRepository = albumRepository;
        }

        public ActionResult Index()
        {
            return View(_albumRepository.GetAll());
        }

        public ActionResult Create()
        {
            Album album = new Album();
            album.DateTime = DateTime.Now;
            _albumRepository.Add(album);

            Directory.CreateDirectory(Folders.MapAlbumPath(album.Id, Server));

            return View("Edit", album);
        }

        public ActionResult Edit(int id)
        {
            return View(_albumRepository.GetById(id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Album album)
        {
            if (!ModelState.IsValid)
                return View(album);

            Album oldAlbum = _albumRepository.GetById(album.Id);
            oldAlbum.DateTime = DateTime.Now;
            oldAlbum.Name = album.Name;

            _albumRepository.Save(oldAlbum);
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string Upload(HttpPostedFileBase FileData, int albumId)
        {
            Album album = _albumRepository.GetById(albumId);

            Guid thumbGuid = Guid.NewGuid();
            Guid guid = Guid.NewGuid();

            Picture picture = new Picture() {Image = guid + ".jpg", Thumb = thumbGuid + ".jpg"};
            album.Pictures.Add(picture);
            _albumRepository.Save(album);

            Image originalImage = Image.FromStream(FileData.InputStream);

            originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            Size fullSize = Resize(originalImage.Size, Folders.MAX_PICTURE_SIZE);
            Size thumbSize = Resize(originalImage.Size, Folders.MAX_THUMB_SIZE);

            Image fullImage = originalImage.GetThumbnailImage(fullSize.Width, fullSize.Height, null, IntPtr.Zero);
            Image thumbImage = originalImage.GetThumbnailImage(thumbSize.Width, thumbSize.Height, null, IntPtr.Zero);

            fullImage.Save(Folders.MapAlbumPath(albumId, Server) + "\\" + picture.Image);
            thumbImage.Save(Folders.MapAlbumPath(albumId, Server) + "\\" + picture.Thumb);

            return picture.Thumb;
        }

        private Size Resize(Size originalSize, int max)
        {
            double percentageUnit = originalSize.Width/100;
            double percentage = max/percentageUnit;

            int newWidth = Convert.ToInt32(percentageUnit*percentage);

            int newHeight = originalSize.Height * newWidth / originalSize.Width;

            Size newSize = new Size {Height = newHeight, Width = newWidth};
            return newSize;
        }

        public JsonResult Delete(int albumId)
        {
            Album album = _albumRepository.GetById(albumId);

            Directory.Delete(Folders.MapAlbumPath(albumId, Server), true);

            _albumRepository.Delete(album);

            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}
